package com.zsy.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zsy.bean.Bis_Business_Order;
import com.zsy.bean.Bis_Custom_User;
import com.zsy.bean.Bis_Nurse_Project;
import com.zsy.bean.Bis_Order_Deposit;
import com.zsy.bean.Bis_Shop_User;
import com.xbw.bean.Bis_Shop;
import com.zsy.service.Bis_Business_OrderService;
import com.zsy.service.Bis_Custom_UserService;
import com.zsy.service.Bis_Nurse_ProjectService;
import com.zsy.service.Bis_Order_DepositService;
import com.xbw.service.ShopService;

/**
 * Ӫҵ������Ϣ��
 * */
@Controller
@RequestMapping("bis_business_order")
public class Bis_Business_OrderController {
	
	@Autowired
	private Bis_Business_OrderService bis_Business_OrderService ; //Ӫҵ������Ϣ��
	@Autowired
	private Bis_Custom_UserService bis_Custom_UserService;//�˿��û���Ϣ�� 
	@Autowired
	private Bis_Nurse_ProjectService bis_Nurse_ProjectService;//������Ŀ��Ϣ��
	@Autowired
	private Bis_Order_DepositService bis_Order_DepositService;//����֧����Ϣ��
	@Autowired
	private ShopService shopService;// ������Ϣ
	
	@RequestMapping("/list")
	public String list(Model model ,HttpServletRequest request ,HttpSession session){
		Map map=initMap(request, session);
		
		List<Bis_Business_Order> listbbo=bis_Business_OrderService.listAll(map);
		for(Bis_Business_Order order:listbbo){
			if(order.getOrder_status().equals("1")){
				Date date=new Date();
				SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
				if(date.after(order.getSubscribe_time())){
					order.setOrder_status("3");
				}
			}
		}
		
		model.addAttribute("listbbo", listbbo);
		List<Bis_Custom_User> listbcu=bis_Custom_UserService.listAll(new HashMap<>());
		model.addAttribute("listbcu", listbcu);
		List<Bis_Nurse_Project> listbnp=bis_Nurse_ProjectService.listAll(new HashMap<>());
		model.addAttribute("listbnp", listbnp);
		List<Bis_Order_Deposit> listbod=bis_Order_DepositService.listAll(new HashMap<>());
		model.addAttribute("listbod", listbod);
		List<Bis_Shop> listbs=shopService.listAll(new HashMap<>());
		model.addAttribute("listbs", listbs);
		return "zsy/bis_business_orderList";
	}
	
	@RequestMapping("/toadd")
	public String toadd(Model model , String order_id ,String mobile ){
	
		
		Bis_Business_Order bis_Business_Order=bis_Business_OrderService.getById(order_id);
		model.addAttribute("bis_Business_Order", bis_Business_Order);
		List<Bis_Custom_User> listbcu	=bis_Custom_UserService.listAll(new HashMap<>());
		model.addAttribute("listbcu", listbcu);
		Bis_Nurse_Project bis_Nurse_Project=bis_Nurse_ProjectService.getByNameprice(new HashMap<>());
		model.addAttribute("bis_Nurse_Project", bis_Nurse_Project);
		return "zsy/bis_business_orderSave";
	}
	
	@RequestMapping("/save")
	public String save(Bis_Business_Order bis_Business_Order ,HttpSession session){
		SimpleDateFormat sdf=new SimpleDateFormat("yyMMddHHmmss");
		String str=sdf.format(new Date());
		Random random=new Random();
		int num= random.nextInt(9999)+1000;
		String id=str+num;
		
		   System.out.println(bis_Business_Order.getBis_Nurse_ProjectProject_id().getProject_id());
			System.out.println(bis_Business_Order.getBis_Nurse_ProjectProject_id().getRmb_price());
		   System.out.println(bis_Business_Order.getOrder_content());
	       Bis_Shop_User bsu=(Bis_Shop_User)session.getAttribute("shopuser");
	       bis_Business_Order.setOrder_id(id);
		   bis_Business_Order.setOrder_type("3");
		   bis_Business_Order.setOrder_status("1");
		   
		   Bis_Shop shop=new Bis_Shop();
		   shop.setShop_id(bsu.getShop_id());
		   bis_Business_Order.setBis_ShopShop_id(shop);
		   bis_Business_Order.setServer_user_id(bsu.getShop_user_id());
		   bis_Business_Order.setCreate_time(new Date());
		
		  bis_Business_OrderService.save(bis_Business_Order);
		  return "redirect:/bis_business_order/list";
	}
	
	
	
	
	@RequestMapping("/tocheck")
	public String tocheck(Model model){
		List<Bis_Nurse_Project> listbnp=bis_Nurse_ProjectService.listAll(new HashMap<>());
		model.addAttribute("listbnp", listbnp);
		return "zsy/bis_nurse_projectList";
	}
	
	@RequestMapping("/toadd1/{project_id}")
	public String toadd1(Model model , String order_id ,String mobile ,@PathVariable("project_id")String project_id ){
		Bis_Business_Order bis_Business_Order=bis_Business_OrderService.getById(order_id);
		model.addAttribute("bis_Business_Order", bis_Business_Order);
		List<Bis_Custom_User> listbcu	=bis_Custom_UserService.listAll(new HashMap<>());
		model.addAttribute("listbcu", listbcu);
		Bis_Nurse_Project bis_Nurse_Project=bis_Nurse_ProjectService.getByProject_id(project_id);
		model.addAttribute("bis_Nurse_Project", bis_Nurse_Project);
		return "zsy/bis_business_orderSave";
	}
	
	//�տ�
	@RequestMapping("/init/{order_id}")
	public String init(@PathVariable("order_id")String order_id ,Model model ){
		Bis_Business_Order bis_Business_Order=bis_Business_OrderService.getById(order_id);
		System.out.println(bis_Business_Order.getBis_Custom_UserCustom_user_id());
		model.addAttribute("bis_Business_Order", bis_Business_Order);
		List<Bis_Business_Order> listbbo=bis_Business_OrderService.listAll(new HashMap<>());
		model.addAttribute("listbbo", listbbo);
		
		return "zsy/bis_business_orderMoney";
	}
	
	@RequestMapping("/update")
	public String update(Bis_Business_Order bis_Business_Order,HttpSession session ,HttpServletRequest request) throws ParseException{
		Date date=new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		bis_Business_Order.setPay_time(sdf.parse(sdf.format(date)));
		//String cash_income =request.getParameter("cash_income ");
		System.out.println(bis_Business_Order.getCash_income()+"现金收入");
		//bis_Business_Order.setCash_income(Double.valueOf(cash_income));
		Object object =session.getAttribute("code");
		System.out.println(object+"--------");
		String checkCode =request.getParameter("checkCode");
		System.out.println(checkCode+"++++++++");
		if (checkCode.equals(object)) {
			bis_Business_OrderService.update(bis_Business_Order);
		}
		
		return "redirect:/bis_business_order/list";
	}
	
	//��װģ����ѯ�ķ���
	public Map initMap(HttpServletRequest request ,HttpSession session){
		Map map=new HashMap<>();
		Bis_Shop_User user=(Bis_Shop_User)session.getAttribute("shopuser");
		map.put("shop_id", user.getShop_id());
		String qorder_id =request.getParameter("qorder_id");
		String qmobile=request.getParameter("qmobile");
		String qusername=request.getParameter("qusername");
		if (qorder_id!=null&&!qorder_id.equals("")) {
			System.out.println("qorder_id:"+qorder_id);
			map.put("qorder_id", qorder_id);
			request.setAttribute("qorder_id", qorder_id);
		}
		if (qmobile!=null&&!qmobile.equals("")) {
			map.put("qmobile", qmobile);
			request.setAttribute("qmobile", qmobile);
		}
		if (qusername!=null&&!qusername.equals("")) {
			map.put("qusername", qusername);
			request.setAttribute("qusername", qusername);
		}
		
		return map;
	}
	
	
	

}
